package com.lixw.langchain.controller;

import cn.hutool.core.date.DateUtil;
import com.lixw.langchain.service.ChatAssistant;
import com.lixw.langchain.service.ChatMemoryAssistant;
import dev.langchain4j.data.message.AiMessage;
import dev.langchain4j.data.message.UserMessage;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @ClassName: ChatMemoryController
 * @Description:
 * @Author: xuweiLi
 * @Create: 2025/8/24 11:40
 **/
@Slf4j
@RestController
public class ChatMemoryController {
    @Resource
    private ChatAssistant chatAssistant;

    @Resource
    private ChatMemoryAssistant chatMessageWindowChatMemory;

    @Resource
    private ChatMemoryAssistant chatTokenWindowChatMemory;

    @GetMapping("/chatmemory/v1")
    public String chat1(){
        String answer1 = chatAssistant.chat("你好，我的名字叫陈浩南");
        log.info("answer1的返回结果：{}", answer1);
        String answer2 = chatAssistant.chat("我的名字叫什么");
        log.info("answer2的返回结果：{}", answer2);
        return "success : "+ DateUtil.now()+"<br> \n\n answer01: "+answer1+"<br> \n\n answer02: "+answer2;
    }

    @GetMapping("/chatmemory/v2")
    public String chat2(){
        UserMessage userMessage = UserMessage.from("你好，我的名字叫陈浩南");
        AiMessage aiMessage1 = chatAssistant.chat(userMessage);
        log.info("answer1的返回结果：{}", aiMessage1.text());

        UserMessage userMessage2 = UserMessage.from("我的名字叫什么");
        //将 第一次的提问 + 第一次的回答 + 第二次的提问 喂给LLM  这种方式属于手动管理上下文
        AiMessage aiMessage2 = chatAssistant.chat(userMessage,aiMessage1,userMessage2);
        log.info("answer2的返回结果：{}", aiMessage2.text());
        return "success : "+ DateUtil.now()+"<br> \n\n answer01: "+aiMessage1.text()+"<br> \n\n answer02: "+aiMessage2.text();
    }

    @GetMapping("/chatmemory/test2")
    public String chatMessageWindow(){
        chatMessageWindowChatMemory.chatWithChatMemory(1L, "你好！我的名字是陈浩南");
        String answer01 = chatMessageWindowChatMemory.chatWithChatMemory(1L, "我的名字是什么");
        log.info("answer01<返回结果>{}", answer01);

        chatMessageWindowChatMemory.chatWithChatMemory(2L, "你好！我的名字是吴彦祖");
        String answer02 = chatMessageWindowChatMemory.chatWithChatMemory(2L, "我的名字是什么");
        log.info("answer02<返回结果>{}", answer02);

        return "chatMessageWindowChatMemory success : "
                + DateUtil.now()+"<br> \n\n answer01: "+answer01+"<br> \n\n answer02: "+answer02;
    }

    @GetMapping("/chatmemory/test3")
    public String chatMessageTokenWindow(){
        chatTokenWindowChatMemory.chatWithChatMemory(1L, "你好！我的名字是陈浩南");
        String answer01 = chatTokenWindowChatMemory.chatWithChatMemory(1L, "我的名字是什么");
        log.info("answer01<返回结果>{}", answer01);

        chatTokenWindowChatMemory.chatWithChatMemory(2L, "你好！我的名字是吴彦祖");
        String answer02 = chatTokenWindowChatMemory.chatWithChatMemory(2L, "我的名字是什么");
        log.info("answer02<返回结果>{}", answer02);

        return "chatMessageWindowChatMemory success : "
                + DateUtil.now()+"<br> \n\n answer01: "+answer01+"<br> \n\n answer02: "+answer02;
    }
}